import {
	defineStore
} from 'pinia';
import {
	ref
} from "vue"

import {
	getUser
} from '@/pages_temp/api/auth.js'


export const useAuthStore = defineStore('auth', () => {
	const id = ref(0)
	const phone = ref('')
	const username = ref('')
	const from = ref('')
	const setUser = (user) => {
		console.log('setUser');
		return new Promise((resolve, reject) => {
			if (user) {
				id.value = user.id
				phone.value = user.phone
				username.value = user.nick_name
				from.value = user.from
				resolve('setUser !')
			} else {
				if (id.value) {
					resolve('setUser !')
				} else {
					getUser().then(res => {
						id.value = res.id
						phone.value = res.phone
						username.value = res.nick_name
						from.value = res.from
						resolve(res)
					}).catch(e => {
						reject(e)
					})
				}
			}
		})
	}
	return {
		id,
		username,
		phone,
		from,
		setUser
	}
})
